package com.cq.hd.app.controller;

import com.cq.hd.app.biz.BusinessSubscribeBiz;
import com.cq.hd.common.annotation.LoginUser;
import com.cq.hd.common.entity.AppLoginUser;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.Throw;
import com.cq.hd.member.api.TbBusinessSubscribeApi;
import com.cq.hd.member.api.dto.BusinessSubscribeUpdateDto;
import com.cq.hd.product.api.vo.UserSubscribeBusinessVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/businessSubscribe")
@Api(tags = "商家订阅相关接口")
public class BusinessSubscribeController {

    @Autowired
    private TbBusinessSubscribeApi businessSubscribeApi;
    @Resource
    private BusinessSubscribeBiz businessSubscribeBiz;

    @ApiOperation(value = "商家订阅和取消订阅")
    @ApiImplicitParam(name = "businessSubscribeUpdateDto", value = "实体", paramType = "body", dataType = "BusinessSubscribeUpdateDto")
    @PutMapping("/updateState")
    public Result<Object> updateState(@RequestBody BusinessSubscribeUpdateDto businessSubscribeUpdateDto, @ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        if (businessSubscribeUpdateDto.getBusinessId() == null || businessSubscribeUpdateDto.getBusinessId() <= 0) {
            Throw.isBusinessException("商家id不能为空");
        }

        businessSubscribeUpdateDto.setUserId(appLoginUser.getUserId());
        return businessSubscribeApi.updateState(businessSubscribeUpdateDto);
    }

    @ApiOperation(value = "用户订阅商家列表")
    @GetMapping("/mySubscribeBusiness")
    public Result<List<UserSubscribeBusinessVo>> mySubscribeBusiness(@ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        if (appLoginUser == null || appLoginUser.getUserId() == null) {
            Throw.isBusinessException("请先登录");
        }
        return businessSubscribeBiz.getUserSubscribeBusinessList(appLoginUser.getUserId());
    }

}
